package refect.Test;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Test {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
        Class c1 = Class.forName("refect.Test.PrivateTest");
        PrivateTest o = (PrivateTest) c1.newInstance();
        Field name = c1.getDeclaredField("name");
        // 暴力反射name
        name.setAccessible(true);
        System.out.println(name.get(o));

        name.set(o,"zs");
        System.out.println(name.get(o));

        Method method = c1.getDeclaredMethod("getName");
//        method.setAccessible(true);
        Object invoke = method.invoke(o);
        System.out.println(invoke);
    }
}
